package cn.lzwei.bilimall.ware.service.impl;

import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lzwei.common.utils.PageUtils;
import cn.lzwei.common.utils.Query;

import cn.lzwei.bilimall.ware.dao.PurchaseDetailDao;
import cn.lzwei.bilimall.ware.entity.PurchaseDetailEntity;
import cn.lzwei.bilimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {
    /**
     * 查询采购需求列表：模糊查询、状态、指定仓库
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        //1.模糊查询
        String key = (String) params.get("key");
        if (!StringUtils.isNullOrEmpty(key)){
            queryWrapper.and(item->{
                item.eq("purchase_id",key).or().eq("id",key).or().eq("sku_id",key);
            });
        }
        //2.状态
        String status = (String) params.get("status");
        if (!StringUtils.isNullOrEmpty(status)){
            queryWrapper.eq("status",status);
        }
        //3.指定仓库
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isNullOrEmpty(wareId)){
            queryWrapper.eq("ware_id",wareId);
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 获取采购单下新建或已分配状态的采购需求：用于领取采购单
     */
    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        //获取采购单下新建或已分配状态的采购需求
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<PurchaseDetailEntity>()
                .eq("purchase_id", id)
                .and(item->{
                            item.eq("status",0).or().eq("status",1);
                        }
                );
        return this.list(queryWrapper);
    }

}